#!/bin/bash

cd "`dirname $0`"

BASEDST="distrib/fsrootdev"

LIBS="libhello libini"
PLUGINS="hello cfgdumper filebrowser"

# colors :
# 31 : removing
# 32 : copying
# 33 : cleaning
# 34 : creating archive

# <-- Deleting old files
    for i in "${BASEDST}/common" "${BASEDST}/plugins" "${BASEDST}/libraries"
    do
        if [ -d "${i}" ]
        then
            echo -e -n "\x1b[1;31mRemoving ${i}...\x1b[0m"
            rm -fr ${i}
            echo -e "\x1b[1;31mok\x1b[0m"
        fi

        mkdir -p ${i}
    done
# -->

# <-- Copying common
    echo -e -n "\x1b[1;32mCopying common/*.h in ${BASEDST}/common/...\x1b[0m"
        for i in `find common -name "*.h"`
        do
            DIR="${BASEDST}/`dirname ${i}`"
            if [ ! -d ${DIR} ]
            then
                mkdir -p ${DIR}
            fi

            cp -f ${i} ${BASEDST}/${i}
        done
    echo -e "\x1b[1;32mok\x1b[0m"

    echo -e -n "\x1b[1;32mCopying common/internals in ${BASEDST}/common/...\x1b[0m"
    cp -fr common/internals ${BASEDST}/common/
    echo -e "\x1b[1;32mok\x1b[0m"
# -->

# <-- Copying libraries
    for i in $LIBS
    do
        echo -e -n "\x1b[1;32mCopying libraries/${i} in ${BASEDST}/libraries/...\x1b[0m"
        cp -r libraries/${i} ${BASEDST}/libraries/
        echo -e "\x1b[1;32mok\x1b[0m"

        echo -e "\x1b[1;33mCleaning ${BASEDST}/libraries/${i}...\x1b[0m"
        make -C ${BASEDST}/libraries/${i} clean
        rm -f ${BASEDST}/libraries/${i}/copy.sh
        echo -e "\x1b[1;33mok\x1b[0m"
    done
# -->

# <-- Copying plugins
    for i in $PLUGINS
    do
        echo -e -n "\x1b[1;32mCopying plugins/${i} in ${BASEDST}/plugins/...\x1b[0m"
        cp -r plugins/${i} ${BASEDST}/plugins/
        echo -e "\x1b[1;32mok\x1b[0m"

        echo -e "\x1b[1;33mCleaning ${BASEDST}/plugins/${i}...\x1b[0m"
        make -C ${BASEDST}/plugins/${i} clean
        rm -f ${BASEDST}/plugins/${i}/copy.sh
        echo -e "\x1b[1;33mok\x1b[0m"
    done
# -->

# <-- Removing backup files
    echo -e -n "\x1b[1;31mRemoving backup files...\x1b[0m"
    ./rmbakfiles ${BASEDST}
    echo -e "\x1b[1;31mok\x1b[0m"
# -->

